package com.example.rfidtouristsclient;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.rfidtouristsclient.client.ClientHelper;
import com.example.rfidtouristsclient.utils.Fillter;

import java.io.IOException;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EditText edittext = findViewById(R.id.id);
        EditText editText2 = findViewById(R.id.ipaddr);
        SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
        edittext.setText(sp.getString("user_id",null));
        editText2.setText(sp.getString("server_ip",null));

    }

    @Override
    protected void onResume() {
        super.onResume();
        EditText edittext = findViewById(R.id.id);
        EditText editText2 = findViewById(R.id.ipaddr);
        SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
        edittext.setText(sp.getString("user_id",null));
        editText2.setText(sp.getString("server_ip",null));

    }

    public void clearCache(View view){
        EditText edittext = findViewById(R.id.id);
        EditText editText2 = findViewById(R.id.ipaddr);
        SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.clear();
        edit.commit();
        SharedPreferences sp2 = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor edit2 = sp2.edit();
        edit2.clear();
        edit2.commit();
        edittext.setText("");
        editText2.setText("");

    }


    public void getUserInfoSaveInFileSystem(View v){
        //存放到该对象保存起来
        EditText edittext = findViewById(R.id.id);
        String id = edittext.getText().toString();
        EditText editText2 = findViewById(R.id.ipaddr);
        String ipaddr = editText2.getText().toString();
        setContentView(R.layout.activity_login);
        SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("user_id",id);
        edit.commit();
        SharedPreferences sp2 = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor edit2 = sp2.edit();
        edit2.putString("server_ip",ipaddr);
        edit2.commit();



        System.out.println(sp.getString("user_id",null));
        System.out.println(sp.getString("server_ip",null));

        TextView tv_warn = findViewById(R.id.tv_warn);

        if(!Fillter.isIDgowell(id)){
            tv_warn.setText("您输入的身份证号不合法!!!");
        }else if(!Fillter.isIPgowell(ipaddr)){
            tv_warn.setText("您输入的ip地址不合法!!!");
        }else{

            //允许在主线程中放行代码
            if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }
                 /*
                 *
                 * 将用这个用户id和服务器通信，如果存在，那么查询出一系列的信息到本地文件userinfo
                 * - 网络通信
                 * - 文件操作
                 * 现在假设已经有积分100分，那么先写一下
                 * */
            ClientHelper clientHelper = new ClientHelper();
            try {
                String integral_Str = clientHelper.startClientByIDAndIp(id, ipaddr);
                //跳转到积分查询界面
                Intent toIntegralSelectActivity = new Intent(LoginActivity.this,IntegralSelectActivity.class);
                toIntegralSelectActivity.putExtra("integral",integral_Str);
                startActivity(toIntegralSelectActivity);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }//成功输入id
    }

}
